package com.ny.service.impl;

import com.ny.entity.Device;
import com.ny.mapper.DeviceMapper;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Override
    public List<Device> findAll() {
        return deviceMapper.findAll();
    }

    @Override
    public List<Device> findByDeviceName(String deviceName) {
        return deviceMapper.findByDeviceName(deviceName);
    }

    @Override
    public List<Device> findByTimeRangeAndStatus(Date startTime, Date endTime, Integer status) {
        return deviceMapper.findByCreatedTimeBetweenAndStatus(startTime, endTime, status);
    }
    
    @Override
    public void deleteByDeviceId(Integer deviceId) {
        deviceMapper.deleteByDeviceId(deviceId);
    }
    
    @Override
    public Device addDevice(Device device) {
        return deviceMapper.save(device);
    }
    
    @Override
    public Device updateDevice(Device device) {
        return deviceMapper.save(device);
    }
    
    @Override
    public void controlDevice(Integer deviceId, Integer status) {
        Device device = deviceMapper.findById(deviceId).orElse(null);
        if (device != null) {
            device.setStatus(status);
            deviceMapper.save(device);
        }
    }
}